import chardet
from pathlib import Path


def detect_encoding(file_path):
    with open(file_path, 'rb') as f:
        result = chardet.detect(f.read())
        encoding = result['encoding']
        return encoding


def list_files_with_encoding(directory):
    directory_path = Path(directory)
    for file in directory_path.glob("**/*.txt"):
        encoding = detect_encoding(file)
        print(f"文件名: {file.name}, 编码: {encoding}")


# 替换为你想要遍历的目录
directory_to_search = "D:\\00\\编码判断"
list_files_with_encoding(directory_to_search)
